package cn.lsnu.modules.watch.controller;

import cn.lsnu.common.interfacetools.interfacecontroller.BaseController;
import cn.lsnu.modules.watch.entity.GaspaymentEntity;
import cn.lsnu.modules.watch.service.RecordWatchService;
import cn.lsnu.modules.watch.service.WatchTaskService;
import cn.lsnu.utils.Global;
import cn.lsnu.utils.Guuid;
import cn.lsnu.utils.UploadTool;
import org.apache.commons.io.FilenameUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import static org.springframework.web.bind.annotation.RequestMethod.*;


@Controller
@RequestMapping("/app/recordwatch")
public class RecordWatchController extends BaseController {

    @Autowired
    private RecordWatchService recordWatchService;

    @Autowired
    private WatchTaskService watchTaskService;

    @RequestMapping("/rwdetail")
    public @ResponseBody
    GaspaymentEntity recordWatchDetial(GaspaymentEntity gaspayment) throws Exception{
        gaspayment = recordWatchService.getEntity(gaspayment);
        return gaspayment;
    }


    @RequestMapping("/save")
    public @ResponseBody
    GaspaymentEntity save(GaspaymentEntity gaspayment) throws Exception{
        recordWatchService.save(gaspayment);
        gaspayment.setCode(Global.STATUE_SUCCESS);
        gaspayment.setMsg("保存成功");
        return gaspayment;
    }

    @RequestMapping(value = {"/uploadimg"},method=RequestMethod.POST)
    public @ResponseBody
    GaspaymentEntity uploadImg(HttpServletRequest request,@RequestParam(value="uploadkey") MultipartFile[] files, GaspaymentEntity gaspayment) throws Exception{
//        String path = request.getSession().getServletContext().getRealPath("/upload/image");
//        //图片上传成功后，将图片的地址写到数据库
//        File fileUrl=new File(path);
//        //如果当前项目里不存在images文件夹，就创建
//        if(!fileUrl.exists()){
//            fileUrl.mkdir();
//        }
//        System.out.println(files.length);
//        for (MultipartFile file : files) {
//            if(file.isEmpty()){
//                break;
//            }else{
//                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
//                String res = sdf.format(new Date());
//                String imgName = UUID.randomUUID().toString().replaceAll("-", "");
//                //获取图片的原始图片的后缀名
//                String ext = FilenameUtils.getExtension(file.getOriginalFilename());
//                File targetFile = new File(path,"/"+res+"."+ext);
//                file.transferTo(targetFile);
//                gaspayment.setImgLocation("/upload/image/"+res+"."+ext);
//            }
//        }
        String databasePath = UploadTool.uploadImg("/upload/image",request);
        gaspayment.setImgLocation(databasePath);
        Date oldRcordDate = gaspayment.getReadDate();
        if(gaspayment.getPayId() == null){
            gaspayment.setPayId(Guuid.uuid());
            recordWatchService.save(gaspayment);
        }else{
            recordWatchService.update(gaspayment);
        }
        gaspayment.setReadDate(oldRcordDate);
        watchTaskService.updateStatus(gaspayment);
        gaspayment.setCode(Global.STATUE_SUCCESS);
        gaspayment.setMsg("保存成功");
        return gaspayment;
    }



}
